#include "RtspCmdLineDeal.h"


CmdLineDeal gCmdLineDeal[] = 
{
	{"CSeq", DoLineCSeq},
	{"User-Agent", DoLineUserAgent},
	{"Accept", DoLineAccept},
	{"Transport", DoLineTransport},
	{"Session", DoLineSession},
	{"Range", DoLineRange},
	{ NULL, NULL}
};

int DoAllLineRequest(char *ppstrBuf,CmdItem *ppCmdItem)
{
	char lstrLine[1024];
	char lstrSourceData[4096];
	char *lpPos;
	char lstrTemp[100];
	int li;

	strcpy(lstrSourceData,ppstrBuf);
	
	/*get first line infomation*/
	lpPos=strstr(lstrSourceData,"\r\n");
	if(NULL==lpPos)
		return ERR_OPTIONS_DATA_END;
	memcpy(lstrLine,lstrSourceData,lpPos-lstrSourceData);
	strcpy(lstrSourceData,lpPos+2);
	
	DoCommandLine(lstrLine,ppCmdItem);
	
	while(0!=strlen(lstrSourceData))
	{

		/*get next line infomation*/
		lpPos=strstr(lstrSourceData,"\r\n");
		if(NULL==lpPos)
			return ERR_OPTIONS_DATA_END;

		memset(lstrLine,0,sizeof(lstrLine));
		memcpy(lstrLine,lstrSourceData,lpPos-lstrSourceData);
		strcpy(lstrSourceData,lpPos+2);
		
		/*search the Head Field deal function*/
		for(li=0;gCmdLineDeal[li].pstrHeaderField;li++)
		{
			//printf("%s\n",gCmdLineDeal[li].pstrHeaderField);
			if(0==memcmp(gCmdLineDeal[li].pstrHeaderField,lstrLine,strlen(gCmdLineDeal[li].pstrHeaderField)))
			{
				//printf("%s\nline:%s\n",gCmdLineDeal[li].pstrHeaderField,lstrLine);
				gCmdLineDeal[li].pHeaderFieldHandler(lstrLine,ppCmdItem);
				break;
			}
		}
	}

/*	printf("DoAllLineRequest seq:%d\n",ppCmdItem->iSeq);*/
	
	return 0;
}

int DoCommandLine(char *ppstrLine,CmdItem *ppCmdItem)
{
	sscanf(ppstrLine,"%s %s %s",ppCmdItem->strCommand,ppCmdItem->strUrl,ppCmdItem->strRtspVersion);
	//printf("command:%s:%s:%s\n",ppCmdItem->strCommand,ppCmdItem->strUrl,ppCmdItem->strRtspVersion);
	return 0;
}

int DoLineCSeq(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
/*	printf("seqLine:%s\n",ppstrLine);*/
	sscanf(ppstrLine,"%s %d",lstrTemp,&ppCmdItem->iSeq);
/*	printf("seq:%d\n",ppCmdItem->iSeq);*/
	return 0;
}

int DoLineUserAgent(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
	
	sscanf(ppstrLine,"%s %s",lstrTemp,ppCmdItem->strUserAgent);
	
	return 0;
}

int DoLineAccept(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
	
	sscanf(ppstrLine,"%s %s",lstrTemp,ppCmdItem->strAccept);
	
	return 0;
}

int DoLineTransport(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
	char *lpPos;
	char lstrTransPort[100];

	printf("################%s#############\n",ppstrLine);
	
	sscanf(ppstrLine,"%s %s",lstrTemp,lstrTransPort);
	//printf("#transoprt#%s#############\n",lstrTransPort);
	strcpy(ppCmdItem->strTransport,lstrTransPort);
	//printf("#transoprt#%s#############\n",lstrTransPort);
	
	//get connect type info(tcp udp)
	lpPos=strchr(lstrTransPort,';');
	memset(lstrTemp,0,sizeof(lstrTemp));
	memcpy(lstrTemp,lstrTransPort,lpPos-lstrTransPort);
	ppCmdItem->iMarkTcp=2;
	if(NULL!=strstr(lstrTemp,"TCP"))
		ppCmdItem->iMarkTcp=1;
	strcpy(lstrTransPort,lpPos+1);
	
	//printf("#transoprt#%s#############\n",lstrTransPort);
	//get port info(tcp or udp)
	while(NULL!=(lpPos=strchr(lstrTransPort,';')))
	{
		//printf("#transoprt#%s#############\n",lstrTransPort);
		memset(lstrTemp,0,sizeof(lstrTemp));
		memcpy(lstrTemp,lstrTransPort,lpPos-lstrTransPort);
		//printf("********lstrtransport:%s********\n",lstrTemp);
		if(0==memcmp(lstrTemp,"client_port",strlen("client_port")))
		{
			//printf("********client_port:%s********\n",lstrTemp);
			sscanf(lstrTemp+12,"%d-%d",&ppCmdItem->iRtpPort,&ppCmdItem->iRtcpPort);
			//printf("********%d:%d********\n",ppCmdItem->iRtpPort,ppCmdItem->iRtcpPort);
			break;
		}
		strcpy(lstrTransPort,lpPos+1);
	}

	//printf("!!!!!!!!!!!!!!!!!!%s!!!!!!!!!!!!!!!!!!!\n",lstrTransPort);
	if(0==memcmp(lstrTransPort,"client_port",strlen("client_port")))
	{
		sscanf(lstrTransPort+12,"%d-%d",&ppCmdItem->iRtpPort,&ppCmdItem->iRtcpPort);
		//printf("!!!!!!!!!!!!!!!!!!!%d:%d!!!!!!!!!!!!!!!!!!\n",ppCmdItem->iRtpPort,ppCmdItem->iRtcpPort);
	}
	//printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
	
	return 0;
}

int DoLineSession(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
	
	sscanf(ppstrLine,"%s %s",lstrTemp,ppCmdItem->strSession);
	
	return 0;
}

int DoLineRange(char *ppstrLine,CmdItem *ppCmdItem)
{
	char lstrTemp[30];
	char *lpPos;
	char lstrRange[100];
	
	sscanf(ppstrLine,"%s %s",lstrTemp,lstrRange);
	
	lpPos=strstr(lstrRange,"npt=");
	
	if(NULL!=lpPos)
	{
		sscanf(lstrRange+4,"%f-%f",&ppCmdItem->iRangeStart,&ppCmdItem->iRangeEnd);
	}
	
	return 0;
}



